<?php include("src/addUser_cs.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="favicon.ico">
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href='css/coolMessage.css' type='text/css' rel='stylesheet' media='screen' />
<title>Adicionar Usuario :: ASSC</title>
<script language="javaScript" type="text/javascript" src="js/jquery.js"></script>
<script language="javaScript" type="text/javascript" src="js/jquery.simplemodal.js"></script>
<script language="javaScript" type="text/javascript" src="js/coolMessage.js"></script>

<script language="javascript" type="text/javascript">
	
	var displayRow = '';
	if (navigator.appName == "Netscape")
		displayRow = 'table-row';
	else if (navigator.appName.indexOf("Explorer") != -1) 
		displayRow = 'block';
	
	var cont = 1;
	var maximum = 5
	function most(){
		document.getElementById("tr_phones").style.display = displayRow;
		if (cont < maximum ){
			cont++;
			$("#div_campo").append("<div id='div_tel_number_"+cont+"'>"+cont+". <input type='text' id='tel_number_"+cont+"' name='tel_number[]' class='text_grv' size='15' maxlength='14'/> <input type='text' id='tel_ext_"+cont+"' name='tel_ext[]' class='text_grv' size='3' maxlength='5'/></div>");
		}
		if(cont >0)
			document.getElementById('btnLess').disabled = 0;
	}
	
	function less(){
		if( cont == 2 )
			document.getElementById("tr_phones").style.display = 'none';
		
		if(cont > 1 ){
			$("#div_tel_number_"+cont).remove();
			cont--;
		}   
		if(cont == 1)
			document.getElementById('btnLess').disabled = 1;
	}
	
	function showMessage(show_msg){
		switch(show_msg){
			case 1://error
				coolMessage('error',"<?php echo $_SESSION["msg_cool"];?>");
			break;
			
			case 2://alert
				coolMessage('alert',"<?php echo $_SESSION["msg_cool"];?>");
			break;
			
			case 3://information
				coolMessage('information',"<?php echo $_SESSION["msg_cool"];?>");
			break;
			
			default:
				
			break;
		}
		return false;
	}
	
	function isMail(valor) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor))
			return (true);
		else
			return (false);
	}
	
	function validate(){
		var msg    = '';
		
		var name = document.getElementById("use_name").value;
		var lastname = document.getElementById("use_lastname").value;
		var email = document.getElementById("use_email").value;
		var login = document.getElementById("use_login").value;
		var p1 = document.getElementById("use_password").value;
		var p2 = document.getElementById("confirm").value;     
		var type = document.getElementById("use_type").value;     
		
		if(name == '') 
			msg='- Por favor entre Nombre. <br />';
		
		if(lastname == '') 
			msg='- Por favor entre apellido. <br />';
		
		if(email == '') 
			msg= msg+'- Por favor entre email. <br />';
		else{
			if ( ! isMail(email) )
				msg= msg+'- Email Erroneo.<br />';
		}
		var a = 1
		while(a <= cont){
			var c = document.getElementById("tel_number_"+a).value
			if(c == '')
				msg += '- Por favor entre telefono '+a+'. <br />';
			a++;
		}
		
		if(login == '') 
			msg= msg+'- Por favor entre el login. <br />';
		else{
			jQuery.ajax({
			type: "POST",
			async : false,
			url: "userDuplicatecheck.php",
			data: 'use_login='+login,
			success: function(result){
				if ( result == 1 ){
					msg += '- El login <strong>'+login+'</strong> no esta disponible.<br />';
					document.getElementById("use_login").value = ""		;		
				}
				else{
					if ( result == -1 )
						msg += 'Validation error.';					
				}
			}
			});
		}
		
		if(type == '') 
			msg = msg+'- Por favor seleccione el tipo de Usuario. <br />';	 
		
		var espacios = false;
		var k = 0;
		
		while (!espacios && (k < p1.length)){
			if (p1.charAt(k) == " ")
				espacios = true;
			k++;
		}
		
		if (espacios)
			msg= msg+'- El password no puede contener espacios. <br />';	 
		
		if (p1.length == 0 || p2.length == 0) 
			msg= msg+'- Por favor entre el Password. <br />';
		
		if (p1 != p2)  
			msg= msg+'- Los campos del password no coinciden. <br />';	 
		
		if(msg==''){
			document.getElementById('form').submit();
			return true;
		}
		else{			
			coolMessage('alert', msg);
			return false;
		}		
	}
	
	function onlyNumbers(e){
		var key;
		if(window.event)
			key = window.event.keyCode;   //IE
		else
			key = e.which;                //firefox
		if (!( (key >= 48 && key <= 57) || key ==8 || key ==9 || key ==0 || key ==46 ))
			return false;
		else
			return true;
	}
	
	

</script>
</head>
<body onload="javascript: showMessage(<?php echo $show_msg;?>)">
<?php $item_select = 1; include("menu.php");?><br />
 <form name="form" id="form" method="post" action="" enctype="multipart/form-data">
 <input type="hidden" name="add" id="add" value="1"/>	

    <div class="content_grv">
    	<table width="90%" align="center" class="shadow">
        	<tr>
            	<td class="title">Adicionar Usuario</td>
            </tr>
            <tr>
            	<td>
                	<table class="tbl_form" align="center">
                    	<tr>
                        	<td width="35%" align="right"><span class="lbl_gray">Nombre:</span></td>
                            <td width="1%">&nbsp;</td>
                            <td><input type="text" name="use_name" id="use_name" class="text_grv" size="21" maxlength="20" /></td>
                        </tr>
                        <tr>
                        	<td align="right"><span class="lbl_gray">Apellido:</span></td>
                            <td>&nbsp;</td>
                            <td><input type="text" name="use_lastname" id="use_lastname" class="text_grv" size="21" maxlength="20" /></td>
                        </tr>                        
                        <tr>
                        	<td align="right"><span class="lbl_gray">Email:</span></td>
                            <td>&nbsp;</td>
                            <td><input type="text" name="use_email" id="use_email" class="text_grv" size="51" maxlength="50"  /></td>
                        </tr>
                        <tr>
                        	<td align="right"><span class="lbl_gray">Telefono:</span><br /><span class="lbl_gray_small">No. Telefono + Ext</span></td>
                            <td>&nbsp;</td>
                            <td>                                    
                                1. <input type="text" name="tel_number[]" id="tel_number_1" class="text_grv" size="15" maxlength="14" />
                                <input type="text" name="tel_ext[]" id="tel_ext_1" class="text_grv" size="3" maxlength="5" />
                                <input type="button" value="+" onClick="javascript: most();" class="button_grv"/>&nbsp;
                                <input type="button" value="-" id="btnLess" onClick="javascript: less()" class="button_grv_cancel" disabled="disabled"/>
                            </td>
                        </tr>
                        <tr id="tr_phones" style="display:none">
                        	<td align="right" valign="top"><span class="lbl_gray">Mas telefonos:</span></td>
                            <td>&nbsp;</td>
                            <td>
                            	<div id="div_campo">
                                
                                </div>
                            </td>
                        </tr>
                        <tr>
                        	<td align="right"><span class="lbl_gray">Login:</span></td>
                            <td>&nbsp;</td>
                            <td><input type="text" name="use_login" id="use_login" class="text_grv" size="21" maxlength="20" /></td>
                        </tr>
                        <tr>
                        	<td align="right"><span class="lbl_gray">Password:</span></td>
                            <td>&nbsp;</td>
                            <td><input type="password" name="use_password" id="use_password" class="text_grv" size="21" maxlength="20" /></td>
                        </tr>
                        <tr>
                            <td align="right"><span class="lbl_gray">Confirmar Password:</span></td>
                            <td>&nbsp;</td>
                            <td><input type="password" name="confirm" id="confirm" class="text_grv" size="21" maxlength="20" /></td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                        	<td align="right"><span class="lbl_gray">Tipo de Usuario:</span></td>
                            <td>&nbsp;</td>
                            <td>
                            	<select name="use_type" id="use_type" class="text_grv">
                                    <option value="" selected="selected" disabled="disabled">Select..</option>
                                    <option value="Adviser">Regular</option>
                                    <option value="Admin">Admin</option>
                            	</select>
                            </td>
                        </tr>                        
                        <tr>
                        	<td colspan="3" align="center">&nbsp;</td>
                        </tr>
                        <tr>
                        	<td colspan="3" align="center">
                            <input type="button" class="button_grv" value="Grabar" onclick="javascript: validate ();"/>
                             <input type="reset" value="Limpiar" class="button_grv_cancel"/>
                            <input type="button" class="button_grv_cancel" value="Cancelar" onClick="javascript:window.location.href='listUser.php';" /></td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
</form>    
</body>
</html>
<?php include('divscoolmessage.php');?>